
// 144.二叉树的前序遍历
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ret;
        function<void(TreeNode*)> dfs = [&](TreeNode* pos)
        {
            ret.push_back(pos->val);
            if(pos->left) dfs(pos->left);
            if(pos->right) dfs(pos->right);
        };
        if(root) dfs(root);
        return ret;
    }
};